package image;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@SuppressWarnings("serial")
public class ImageWebService2Servlet extends HttpServlet {
	byte[] imagebytes = null;
    String filename = "";
    String width = "";
    String height = "";
    String left = "";
    String top = "";
    String right = "";
    String bottom = "";
    String addrReceiver = "";
    String addrSender = "";
    String degree = "";
    String sendcheckbox = null;
    String resizecheckbox = null;
    String rotatecheckbox = null;
    String cropcheckbox = null;
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		try {
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iterator = upload.getItemIterator(req);

            // for each of the input parameters.
            while (iterator.hasNext()) {
              FileItemStream item = iterator.next();
              if (item.isFormField()) {
                  // get the values of the regular form inputs, and put them
                  // in the appropriate variables.
            	  if(item.getFieldName().compareTo("resizecheckbox") == 0) {
                      resizecheckbox = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("width") == 0) {
                      width = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("height") == 0) {
                      height = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("cropcheckbox") == 0) {
                      cropcheckbox = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("left") == 0) {
                      left = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("top") == 0) {
                      top = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("right") == 0) {
                      right = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("bottom") == 0) {
                      bottom = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("rotatecheckbox") == 0) {
                      rotatecheckbox = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("degree") == 0) {
                      degree = getParameterValue(item);
                  }else if(item.getFieldName().compareTo("sendcheckbox")==0){
                	  sendcheckbox = getParameterValue(item);
                  }else if (item.getFieldName().compareTo("addrSender")==0){
                	  addrSender = getParameterValue(item);
                  }else if (item.getFieldName().compareTo("addrReceiver")==0){
                	  addrReceiver = getParameterValue(item);
                  }
              }else{
                  InputStream stream = item.openStream();
                  filename = item.getName();
                  
                  // Read the file into a byte array.
                  ByteArrayOutputStream out = new ByteArrayOutputStream();
                  byte[] buf = new byte[8192];
                  int len = 0;
                  while(-1 != (len=stream.read(buf))) {
                      out.write(buf, 0, len); 
                  }  
                  imagebytes = out.toByteArray();
              }
            }
          } catch (Exception ex) {
            throw new ServletException(ex);
          }
          if(imagebytes != null && imagebytes.length > 0 && filename.matches(".*\\.(jpg|png|gif)$")) 
          {
        	  if(resizecheckbox != null && cropcheckbox == null  && rotatecheckbox == null && sendcheckbox == null){
        		  ResizeImage resizeImage = new ResizeImage();
		          byte[] newImageData = resizeImage.resizeImage(imagebytes,width,height);
		          // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData, 0, newImageData.length);
        	  }
        	  if(resizecheckbox == null && cropcheckbox != null  && rotatecheckbox==null && sendcheckbox == null ){
        		  CropImage cropImage = new CropImage();
        		  byte[] newImageData = cropImage.cropImage(imagebytes, left, top, right, bottom);
        		  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData, 0, newImageData.length);
        	  }
        	  if(resizecheckbox == null && cropcheckbox == null  && rotatecheckbox != null && sendcheckbox == null ){
        		  RotateImage rotateImage = new RotateImage();
        		  byte[] newImageData = rotateImage.rotateImage(imagebytes,degree);
        		  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData, 0, newImageData.length);
        	  }
        	  if(resizecheckbox == null && cropcheckbox == null && rotatecheckbox==null  && sendcheckbox != null){
        		  SentEmail sendEmail = new SentEmail();
	        	  sendEmail.sendEmail(imagebytes, addrSender, addrReceiver);
	        	  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(imagebytes, 0, imagebytes.length);
        	  }
        	  //*********************************End choice of one service*************************************************
        	  if(resizecheckbox != null && cropcheckbox != null && sendcheckbox == null && rotatecheckbox == null ){
        		  CropImage cropImage = new CropImage();
        		  byte[] newImageData = cropImage.cropImage(imagebytes, left, top, right, bottom);
        		  ResizeImage resizeImage = new ResizeImage();
		          byte[] newImageData1 = resizeImage.resizeImage(newImageData,width,height);
		          // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData1, 0, newImageData1.length);
        	  }
        	  if(resizecheckbox != null && cropcheckbox == null  && rotatecheckbox != null && sendcheckbox == null ){
        		  ResizeImage resizeImage = new ResizeImage();
		          byte[] newImageData = resizeImage.resizeImage(imagebytes,width,height);
		          RotateImage rotateImage = new RotateImage();
        		  byte[] newImageData1 = rotateImage.rotateImage(newImageData,degree);
        		  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData1, 0, newImageData1.length);
        	  }
        	  if(resizecheckbox != null && cropcheckbox == null && rotatecheckbox == null  && sendcheckbox != null ){
		          ResizeImage resizeImage = new ResizeImage();
		          byte[] newImageData = resizeImage.resizeImage(imagebytes,width,height);
	        	  SentEmail sendEmail = new SentEmail();
	        	  sendEmail.sendEmail(newImageData, addrSender, addrReceiver);
	        	  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData, 0, newImageData.length);
	          }
        	  if(resizecheckbox == null && cropcheckbox != null && rotatecheckbox != null && sendcheckbox == null  ){
        		  CropImage cropImage = new CropImage();
        		  byte[] newImageData = cropImage.cropImage(imagebytes, left, top, right, bottom);
        		  RotateImage rotateImage = new RotateImage();
        		  byte[] newImageData1 = rotateImage.rotateImage(newImageData,degree);
        		  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData1, 0, newImageData1.length);
        	  }
        	  if(resizecheckbox == null && cropcheckbox != null && rotatecheckbox == null && sendcheckbox != null  ){
        		  CropImage cropImage = new CropImage();
        		  byte[] newImageData = cropImage.cropImage(imagebytes, left, top, right, bottom);
        		  SentEmail sendEmail = new SentEmail();
	        	  sendEmail.sendEmail(newImageData, addrSender, addrReceiver);
	        	  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData, 0, newImageData.length);
        	  }
        	  if(resizecheckbox == null && cropcheckbox == null && rotatecheckbox != null && sendcheckbox != null  ){
        		  RotateImage rotateImage = new RotateImage();
        		  byte[] newImageData = rotateImage.rotateImage(imagebytes,degree);
        		  SentEmail sendEmail = new SentEmail();
	        	  sendEmail.sendEmail(newImageData, addrSender, addrReceiver);
	        	  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData, 0, newImageData.length);
        	  }
        	  //*****************************************End choice of two services*************************************
        	  if(resizecheckbox != null && cropcheckbox != null  && rotatecheckbox != null && sendcheckbox == null ){
        		  ResizeImage resizeImage = new ResizeImage();
		          byte[] newImageData = resizeImage.resizeImage(imagebytes,width,height);
		          CropImage cropImage = new CropImage();
        		  byte[] newImageData1 = cropImage.cropImage(newImageData, left, top, right, bottom);
        		  RotateImage rotateImage = new RotateImage();
        		  byte[] newImageData2 = rotateImage.rotateImage(newImageData1 ,degree);
        		  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData2, 0, newImageData2.length);
        	  }
        	  if(resizecheckbox != null && cropcheckbox != null  && rotatecheckbox == null && sendcheckbox != null ){
        		  ResizeImage resizeImage = new ResizeImage();
		          byte[] newImageData = resizeImage.resizeImage(imagebytes,width,height);
		          CropImage cropImage = new CropImage();
        		  byte[] newImageData1 = cropImage.cropImage(newImageData, left, top, right, bottom);
        		  SentEmail sendEmail = new SentEmail();
	        	  sendEmail.sendEmail(newImageData1, addrSender, addrReceiver);
	        	  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData1, 0, newImageData1.length);
        	  }
        	  if(resizecheckbox != null && cropcheckbox == null  && rotatecheckbox != null && sendcheckbox != null ){
        		  ResizeImage resizeImage = new ResizeImage();
		          byte[] newImageData = resizeImage.resizeImage(imagebytes,width,height);
		          RotateImage rotateImage = new RotateImage();
        		  byte[] newImageData1 = rotateImage.rotateImage(newImageData, degree);
        		  SentEmail sendEmail = new SentEmail();
	        	  sendEmail.sendEmail(newImageData1, addrSender, addrReceiver);
	        	  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData1, 0, newImageData1.length);
        	  }
        	  if(resizecheckbox == null && cropcheckbox != null  && rotatecheckbox != null && sendcheckbox != null ){
        		  CropImage cropImage = new CropImage();
        		  byte[] newImageData = cropImage.cropImage(imagebytes, left, top, right, bottom);
        		  RotateImage rotateImage = new RotateImage();
        		  byte[] newImageData1 = rotateImage.rotateImage(newImageData ,degree);
        		  SentEmail sendEmail = new SentEmail();
	        	  sendEmail.sendEmail(newImageData1, addrSender, addrReceiver);
	        	  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData1, 0, newImageData1.length);
        	  }
        	  //**********************************End choice of three services*******************************************
        	  if(resizecheckbox != null && cropcheckbox != null  && rotatecheckbox != null && sendcheckbox != null ){
        		  ResizeImage resizeImage = new ResizeImage();
		          byte[] newImageData = resizeImage.resizeImage(imagebytes,width,height);
		          RotateImage rotateImage = new RotateImage();
        		  byte[] newImageData1 = rotateImage.rotateImage(newImageData,degree);
        		  CropImage cropImage = new CropImage();
        		  byte[] newImageData2 = cropImage.cropImage(newImageData1, left, top, right, bottom);
        		  SentEmail sendEmail = new SentEmail();
	        	  sendEmail.sendEmail(newImageData2, addrSender, addrReceiver);
	        	  // Write out the new image to the browser.
		          resp.setContentType(contentTypeFromFilename(filename));
		          resp.getOutputStream().write(newImageData2, 0, newImageData2.length);
        	  }
          }
          else{
        	  resp.setContentType("text/html");
              resp.getWriter().print(width + " " + height);
              resp.getOutputStream().write(imagebytes,0,imagebytes.length);
          }
	}
	//*****************************Method getParameterValue**********************************
	private String getParameterValue(FileItemStream fi) throws IOException {
        InputStream stream = fi.openStream();
        // Read the file into a byte array.
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[8192];
        int len = 0;
        while(-1 != (len=stream.read(buf))) {
            out.write(buf, 0, len);
        }
        return out.toString();
   }
	//******************************Method contenTypeFromFilename****************************
	private String contentTypeFromFilename(String filename) {
        if(filename.matches("\\.jpg")) {
            return "image/jpeg";
        } else if(filename.matches("\\.png")) {
            return "image/png";
        } else if(filename.matches("\\.gif")) {
            return "image/gif";
        } else {
          // Default of JPEGs?
          return "image/jpeg";
        }
  }
}
